<script>
import SearchBuilding from '@/views/park/building/components/searchBuilding/searchBuilding.vue'
import { mapGetters } from 'vuex'
import BuildingForm from '@/views/park/building/components/buildingForm/buildingForm.vue'

export default {
  name: 'building',
  data() {
    return {
      /* 获取楼宇列表的分页参数 */
      pageParams: {
        // 当前页
        page: 1,
        // 每页大小
        pageSize: 10,
        // 楼宇名称模糊查询
        name: undefined
      },
      /* 加载楼宇的状态 */
      loading: false,
      /* 是否显示添加或者编辑楼宇的弹框 */
      dialogVisible: false,
      /* 是否是编辑模式 */
      edit: false
    }
  },

  methods: {
    /* 格式化楼宇状态的回调函数 */
    formatterStatus(row) {
      const MAP = { 0: '空置中', 1: '租赁中' }
      return MAP[row.status] || '未知'
    },
    /* 获取楼宇列表的回调函数 */
    async fetchBuildingList(pageParams) {
      try {
        this.loading = true
        await this.$store.dispatch('buildingStore/fetchBuildingList', pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /* 分页器size发生变化的回调函数 */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchBuildingList(this.pageParams)
    },
    /* 分页器page发生变化的回调函数 */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchBuildingList(this.pageParams)
    },
    /* 根据pageParams的name楼宇名称进行模糊搜索 */
    handleSearch() {
      this.fetchBuildingList(this.pageParams)
    },
    /* 点击添加楼宇按钮的回调函数 */
    handleAdd() {
      this.edit = false
      this.dialogVisible = true
    },
    /* 点击编辑楼宇按钮的回调函数 */
    async handleEdit(row) {
      this.edit = true
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.buildingForm.findBuilding(row.id)
      })
    },
    /* 点击删除楼宇按钮的回调函数 */
    async handleDelete(row) {
      const confirm = await this.$confirm(`删除${row.name}, 是否继续?`, '提示').catch(error => error)
      if (confirm === 'cancel') return
      try {
        const deleteResult = await this.$store.dispatch('buildingStore/deleteBuilding', row.id)
        await this.fetchBuildingList(this.pageParams)
        this.$message.success(deleteResult)
      } catch (error) {
        this.$message.error(error.message)
      }
    },

    handleSuccess() {
      this.dialogVisible = false
      this.fetchBuildingList(this.pageParams)
    }
  },
  computed: {
    ...mapGetters('buildingStore', ['buildingList', 'total']),
    /* 弹框的标题 */
    dialogTitle() {
      return this.edit ? '编辑楼宇' : '添加楼宇'
    }
  },
  created() {
    this.fetchBuildingList(this.pageParams)
  },
  components: {
    BuildingForm,
    SearchBuilding
  }
}
</script>

<template>
  <div class="building">
    <!--查询楼宇-->
    <el-card shadow="never">
      <search-building :page-params="pageParams" @search="handleSearch"></search-building>
    </el-card>
    <!--楼宇列表-->
    <el-card shadow="never">
      <el-button size="small" type="primary" icon="el-icon-plus" @click="handleAdd">添加楼宇</el-button>
      <el-table border stripe size="small" :header-cell-style="{backgroundColor:'#f4f6f8'}" v-loading="loading" element-loading-text="拼命加载中" element-loading-background="rgb(255, 255, 255)" :data="buildingList">
        <el-table-column label="序号" type="index" align="center"></el-table-column>
        <el-table-column label="楼宇名称" prop="name" align="center"></el-table-column>
        <el-table-column label="楼宇编号" prop="id" align="center"></el-table-column>
        <el-table-column label="层数" prop="floors" align="center"></el-table-column>
        <el-table-column label="在管面积(m²)" prop="area" align="center"></el-table-column>
        <el-table-column label="物业费(元/m²)" prop="propertyFeePrice" align="center"></el-table-column>
        <el-table-column label="状态" prop="status" align="center" :formatter="formatterStatus"></el-table-column>
        <el-table-column label="操作" prop="" align="center">
          <template #default="{row}">
            <el-button size="mini" type="warning" icon="el-icon-edit" @click="handleEdit(row)">编辑楼宇</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(row)">删除楼宇</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页器-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[10, 20, 30, 40]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background></el-pagination>
    </el-card>
    <!--添加楼宇或者编辑楼宇的弹框-->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="35%" @close="$refs.buildingForm.resetFormData()">
      <!--表单组件-->
      <building-form @success="handleSuccess" @cancel="dialogVisible=false" ref="buildingForm"></building-form>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.building {
  padding: 10px 0;

  .el-card {
    margin-bottom: 10px;

    .el-table {
      margin: 10px 0;
    }

    .el-pagination {
      display: flex;
      justify-content: center;
    }

  }

  ::v-deep .el-dialog__body {
    display: flex;
    justify-content: center;
  }
}
</style>
